package shmged.com.shlcm.projects;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.jsp.PageContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import shmged.com.shlcm.utils.CmWTContainerHelper;
import shmged.com.shlcm.utils.CmWTUtil;
import wt.content.ApplicationData;
import wt.content.ContentRoleType;
import wt.content.ContentServerHelper;
import wt.doc.DocumentType;
import wt.doc.WTDocument;
import wt.folder.Folder;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerRef;
import wt.type.TypeDefinitionReference;
import wt.type.TypedUtility;

public class CmDefaultCreateObject extends CmAbstractSaveData {
	
	private static Log log = LogFactory.getLog(CmDefaultCreateObject.class);

	public CmDefaultCreateObject(PageContext pc) throws Exception {
		super(pc, true);
	}

	@Override
	public WTDocument getWTDocument() throws Exception {
		WTDocument doc = null;
		if(null == oid || oid.trim().length() == 0){
			doc = WTDocument.newWTDocument();
			doc.setDocType(DocumentType.getDocumentTypeDefault());
			doc.setContainerReference(getWTContainerRef());
			if(null != title){
				doc.setTitle(title);
			}
			if(null != name){
				doc.setName(name);
			}
			if(null != number){
				doc.setNumber(number);
			}
			if(null != description){
				doc.setDescription(description);
			}
		}
		return doc;
	}

	@Override
	public Properties getIBAs() throws Exception {
		Properties ibas = new Properties();
		Enumeration<?> names = su.getParameterNames();
		if(null != names){
			String tname = null;
			String tvalue = null;
			String endName = null;
			int index = "IBA".length();
			int index2 = "DATEIBA".length();
			while(names.hasMoreElements()){
				tname = (String)names.nextElement();
				tvalue = su.getParameter(tname);
				if(null == tvalue || tvalue.trim().length() == 0){
					continue;
				}
				if(tname.startsWith("IBA")){
					endName = tname.substring(index);
				}else if(tname.startsWith("DATEIBA")){//yyyy-MM-dd HH:mm:ss z
					endName = tname.substring(index2);
					Timestamp time = Timestamp.valueOf(tvalue+" 09:00:00.0");
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
					tvalue = sdf.format(new Date(time.getTime()));
				}else{
					continue;
				}
				ibas.put(endName, tvalue);
			}
		}
		return ibas;
	}

	@Override
	public WTContainer getWTContainer() throws Exception {
//		if(null == containerName || containerName.trim().length() == 0){
//			containerName = "project001";
//		}
//		return CmWTContainerHelper.getWTContainer(containerType, containerName);
    return shmged.com.shlcm.utils.CmCreateDocHelper.getWTContainer("project001");
	}

	@Override
	public WTContainerRef getWTContainerRef() throws Exception {
		return WTContainerRef.newWTContainerRef(getWTContainer());
	}

	@Override
	public Folder getFolder() throws Exception {
		if(null == folderPath){
			return CmWTContainerHelper.getFolder("/Default", getWTContainerRef());
		}
		return CmWTContainerHelper.getFolder(folderPath, getWTContainerRef());
	}

	@Override
	public TypeDefinitionReference getTypeDefinitionReference()
			throws Exception {
		if(null == softType){
			return null;
		}
		String typeId = CmWTUtil.qualifyTypeId(softType);
   	    TypeDefinitionReference tdr = TypedUtility.getTypeDefinitionReference(typeId);
		return tdr;
	}

	@Override
	public void createFiles(ArrayList<CmAmFile> files) throws Exception {
		CmAmFile file = null;
		 for(int i =0 ;i < files.size() ;i++){
        	 file = files.get(i);
        	 File temp = file.getFile();
        	 if(!temp.exists()){
        		 continue;
        	 }
        	 ApplicationData data = ApplicationData.newApplicationData(doc);
        	 if ("inp_primary_file".equals(file.getFieldName())) {
        		 data.setRole(ContentRoleType.PRIMARY);
        	 } else{
        		 data.setRole(ContentRoleType.SECONDARY);
        	 }
        	 data.setFileName(file.getFileName());
        	 data.setUploadedFromPath(file.getFilePathName());
        	 InputStream is = new FileInputStream(temp);
        	 try {
        		 data = ContentServerHelper.service.updateContent(doc, data, is);
        	 } finally {
        		 is.close();
        	 }
         }
	}

	@Override
	public void ibaFileName(ArrayList<CmAmFile> files, Properties ibas)
			throws Exception {
		CmAmFile file = null;
		String field = null;
		String tname = null;
		for(int i = 0 ;i < files.size();i ++){
			file = files.get(i);
			field = file.getFieldName();
			if(null == field){
				continue;
			}
			if(!field.startsWith("FILE_IBA_")){
				continue;
			}
			tname = field.substring("FILE_IBA_".length());
			System.out.println("----"+tname+"----"+file.getFileName());
			ibas.put(tname, file.getFileName());
		}
	}
}
